﻿using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using XFControls.Utils;
#pragma warning disable 1591

namespace XFControls.XFPanels
{
    public partial class XFPanelAlphabetBar : Panel
    {
        #region Delegates

        public delegate void LetterChangedEventHandler(char newLetter);

        #endregion

        private readonly char[] _letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
        private char _selLetter;
        private Rectangle _number;
        private const int _padding = 15;
        private byte[] buffer;
        private Rectangle rec;

        public XFPanelAlphabetBar()
        {
            InitializeComponent();
            Width = XFGraphics.GetScreenResolutionNotRotated() == ScreenResolution.QVGA ? 25 : 50;
            _selLetter = ' ';


            var myAssembly = GetType().Assembly;
            string ns = myAssembly.GetName().Name;

            string Uri = ns + ".Resources.alphabar.png";

            if (XFGraphics.ImageCache.Keys.Any(k => k.StartsWith("alphabar")))
            {
                string key = XFGraphics.ImageCache.Keys.First(k => k.StartsWith("alphabar"));

                using (var file = File.OpenRead(XFGraphics.ImageCache[key]))
                {
                    buffer = new byte[file.Length];
                    file.Read(buffer, 0, (int)file.Length);
                    return;
                }
            }

            var ms = (MemoryStream)myAssembly.GetManifestResourceStream(Uri);
            //var ms = XFGraphics.GetMemoryAssembly(this, uri);)


            if (ms == null)
            {
                myAssembly = Assembly.GetExecutingAssembly();
                ns = myAssembly.GetName().Name;
                ms = (MemoryStream)myAssembly.GetManifestResourceStream(Uri);
                //var ms = XFGraphics.GetMemoryAssembly(this, uri);)
                if (ms == null) return;
            }

            buffer = ms.GetBuffer();
        }

        public new int Width { get; set; }
        public int XOffset { get; set; }
        public event LetterChangedEventHandler LetterChanged;
        public Color BackgroundStart = Color.Navy;
        public Color BackgroundEnd = Color.Blue;
        public Color BackgroundBorder = Color.FromArgb(20, 20, 20);
        public int BackgroundBorderSize = 5;


        public void PaintAlphaBar(Graphics g)
        {
            if (rec == Rectangle.Empty)
                rec = new Rectangle(XOffset - Width, 0, Width, Height);

            using (var b = new SolidBrush(Color.DarkGray))
                g.FillRectangle(b, XOffset - Width, 0, Width, Height);

            if (buffer != null)
                XFGraphics.DrawMemoryStream(g, rec, buffer, false);

            if (_selLetter == ' ') return;

            g.DrawRoundedRectangleBorderGradiantFill(BackgroundStart, BackgroundEnd, BackgroundBorder, _number, 10, BackgroundBorderSize);
            using (var f = new Font(FontFamily.GenericMonospace, 24, FontStyle.Bold))
            {
                using (var b = new SolidBrush(Color.White))
                    g.DrawString(_selLetter.ToString(), f, b, _number.X + _padding, _number.Y + _padding);
            }
        }

        public void PanelMouseDown(MouseEventArgs e)
        {
            double itmHeight = Height / (double)_letters.Length;

            int letterIndex = 0;
            for (; (letterIndex + 1) * itmHeight < e.Y; letterIndex++) { }

            _selLetter = _letters[letterIndex];
            int x = Screen.PrimaryScreen.WorkingArea.Width >> 1;
            int y = 200;

            using (var f = new Font(FontFamily.GenericMonospace, 24, FontStyle.Bold))
            {
                Size tLength = f.MeasureString(_selLetter.ToString(), _number);

                x -= tLength.Width >> 1;
                y -= tLength.Height >> 1;

                _number = new Rectangle(x - _padding, y - _padding, tLength.Width + (_padding << 1), tLength.Height + (_padding << 1));
            }
            if (Parent != null)
                Parent.Invalidate();
        }

        public void PanelMouseMove(MouseEventArgs e)
        {
            double itmHeight = Height / (double)_letters.Length;

            int letterIndex = 0;
            for (; (letterIndex + 1) * itmHeight < e.Y; letterIndex++) { }

            if (letterIndex > _letters.Length)
                _selLetter = _letters.Last();
            else
                _selLetter = _letters[letterIndex];

            if (Parent != null)
                Parent.Invalidate();
        }

        public void PanelMouseUp(MouseEventArgs e)
        {
            if (LetterChanged != null)
            {
                LetterChanged(_selLetter);
            }
            _selLetter = ' ';

            if (Parent != null)
                Parent.Invalidate();
        }
    }
}
#pragma warning restore 1591
